package com.eboss2.sbs.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.API;
import com.eboss2.sbs.http.HttpCallback;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.GoodsAttribute;
import com.eboss2.sbs.model.TempGgInfo;
import com.eboss2.sbs.model.TempGoodsDetailInfo;
import com.eboss2.sbs.model.WalletAccountInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.TimeUtil;
import com.mytools.tool.views.MyListView;
import com.tool.utils.utils.BMapUtil;
import com.tool.utils.utils.SPUtils;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.view.MoneyEditText;
import com.tool.utils.view.RTextView;

import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyAddServiceStoreActivity extends BaseActivity implements View.OnClickListener, View.OnLongClickListener {

    @BindView(R.id.et_goods_name)
    EditText etGoodsName;
    @BindView(R.id.et_kc)
    EditText etKc;
    @BindView(R.id.et_price)
    MoneyEditText etPrice;
    @BindView(R.id.et_gh_price)
    MoneyEditText etGhPrice;
    @BindView(R.id.et_member_price)
    MoneyEditText etMemberPrice;
    @BindView(R.id.et_unmember_price)
    MoneyEditText etUnmemberPrice;
    //    @BindView(R.id.tv_yfmb)
//    TextView tvYfmb;
    @BindView(R.id.et_limit_num)
    EditText etLimitNum;
    @BindView(R.id.tv_date_choose)
    TextView tvDateChoose;
    @BindView(R.id.id_check_one)
    CheckBox idCheckOne;
    @BindView(R.id.id_check_two)
    CheckBox idCheckTwo;
    @BindView(R.id.id_md)
    RadioButton idMd;
    @BindView(R.id.id_zb)
    RadioButton idZb;
    @BindView(R.id.et_settle_bl)
    EditText etSettleBl;
    @BindView(R.id.ll_settle_bl)
    LinearLayout llSettleBl;
    private ArrayList<ImageItem> images1 = new ArrayList<>();
    private ArrayList<ImageItem> images2 = new ArrayList<>();
    private ArrayList<ImageItem> images3 = new ArrayList<>();
    private String[] images = new String[3];
    private TempGgInfo tempGgInfo;
    private TempGoodsDetailInfo tempGoodsDetailInfo;
    private RelativeLayout ll_spgg;
    private RelativeLayout rl_one_category;
    private TextView tv_one_category;
    private TextView tv_two_category;

    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img_show1;
    private ImageView img_show2;
    private ImageView img_show3;
    private ImageView img_close1;
    private ImageView img_close2;
    private ImageView img_close3;

    private TextView tv_cancel;
    private TextView tv_save;
//    private TextView tv_yfmb;

    //    private long emsMethodId;
    private String firstCategoryId;
    private String twoCategoryId;


    private List<GoodsAttribute> goodsAttributes_data = new ArrayList<GoodsAttribute>();
    private MyListView listview;
    private AdapterGoodsAttributeList adapterGoodsAttributeList;
    private RTextView tv_attribute_add;

    private RelativeLayout ll_store_detail;
    private RelativeLayout ll_jfdh;
//    private RelativeLayout rl_yfmb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_add_service_store);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
//		initImagePicker();
//		initData();

    }

    private void initData() {
        SPUtils.clear(Config.GG_FILE_NAME, mContext);
    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "新增服务商品");
        findViewById(R.id.titleback_btn_back).setOnClickListener(this);

        ll_spgg = relativeLayout(R.id.ll_spgg);
        ll_spgg.setOnClickListener(this);

        tv_cancel = textView(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
        tv_save = textView(R.id.tv_save);
        tv_save.setOnClickListener(this);

        img1 = imageView(R.id.img_1);
//        img1.setOnClickListener(this);
//        img1.setOnLongClickListener(this);
        img2 = imageView(R.id.img_2);
//        img2.setOnClickListener(this);
//        img2.setOnLongClickListener(this);
        img3 = imageView(R.id.img_3);
//        img3.setOnClickListener(this);
//        img3.setOnLongClickListener(this);


        img_show1 = imageView(R.id.img_show1);
        img_show2 = imageView(R.id.img_show2);
        img_show3 = imageView(R.id.img_show3);


        findViewById(R.id.ll_img1).setOnClickListener(this);
        findViewById(R.id.ll_img2).setOnClickListener(this);
        findViewById(R.id.ll_img3).setOnClickListener(this);



        ll_store_detail = relativeLayout(R.id.ll_store_detail);
        ll_store_detail.setOnClickListener(this);

        rl_one_category = relativeLayout(R.id.rl_one_category);
        rl_one_category.setOnClickListener(this);

        ll_jfdh = relativeLayout(R.id.ll_jfdh);
        ll_jfdh.setOnClickListener(this);

//        rl_yfmb = relativeLayout(R.id.rl_yfmb);
//        rl_yfmb.setOnClickListener(this);
//        tv_yfmb = textView(R.id.tv_yfmb);

        tv_one_category = textView(R.id.tv_one_category);
        tv_two_category = textView(R.id.tv_two_category);

        tvDateChoose.setOnClickListener(this);

        img_close1 = imageView(R.id.img_close1);
        img_close1.setOnClickListener(this);
        img_close2 = imageView(R.id.img_close2);
        img_close2.setOnClickListener(this);
        img_close3 = imageView(R.id.img_close3);
        img_close3.setOnClickListener(this);

        tv_attribute_add = (RTextView) textView(R.id.tv_attribute_add);
        tv_attribute_add.setOnClickListener(this);

        listview = findViewById(R.id.listview);
        adapterGoodsAttributeList = new AdapterGoodsAttributeList(mContext, goodsAttributes_data, new AdapterGoodsAttributeList.ListenerDel() {
            @Override
            public void del(int position) {
                getEditText(goodsAttributes_data, listview, -1);
                goodsAttributes_data.remove(position);
                adapterGoodsAttributeList.notifyDataSetChanged();
            }
        });
        listview.setAdapter(adapterGoodsAttributeList);


        idMd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    llSettleBl.setVisibility(View.VISIBLE);
                } else {
                    llSettleBl.setVisibility(View.GONE);
                }
            }
        });

    }

    private void getEditText(List<GoodsAttribute> goodsAttributes, MyListView listview, int position) {

        for (int i = 0; i < listview.getChildCount(); i++) {
            if (i != position) {
                LinearLayout layout = (LinearLayout) listview.getChildAt(i);
                EditText et_name = (EditText) layout.findViewById(R.id.et_goods_name);
                EditText et_value = (EditText) layout.findViewById(R.id.et_goods_value);

                goodsAttributes.get(i).setGoodsName(et_name.getText().toString().trim());
                goodsAttributes.get(i).setGoodsValue(et_value.getText().toString().trim());
            }
        }
    }


    private void getdata() {
        Map<String, Object> params = new HashMap<String, Object>();
//		params.put("sid", mApp.getLoginEntity().getSid());
        API.http_post(this, API.WALLET_ACCOUNT, params, new HttpCallback<WalletAccountInfo>(this) {
            @Override
            public void onSuccess(int statusCode, WalletAccountInfo data) {

            }

            @Override
            public void onFailure(int statusCode, String message) {
                super.onFailure(statusCode, message);
            }

            @SuppressWarnings({"unchecked", "rawtypes"})
            @Override
            public TypeToken getTypeToken() {
                return new TypeToken<WalletAccountInfo>() {
                };
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        ImagePicker.getInstance().setMultiMode(false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
//            case R.id.rl_yfmb:
//                startActivityForResult(new Intent(mContext, MyEmsModleyListActivity.class), 700);
//                break;
            case R.id.rl_one_category:
                startActivityForResult(new Intent(mContext, MyCategoryListActivity.class).putExtra("type", 1), 600);
                break;
            case R.id.ll_jfdh:
                ToastUtils.showShort(mContext, "请去电脑后台完善信息");
                break;
            case R.id.ll_spgg:
                ToastUtils.showShort(mContext, "请去电脑后台完善信息");

//				startActivityForResult(new Intent(mContext, MySpggActivity.class).putExtra(Config.GG_FILE_NAME, tempGgInfo), 400);
                break;
            case R.id.ll_store_detail:
                ToastUtils.showShort(mContext, "请去电脑后台完善信息");

//				startActivityForResult(new Intent(this, WxDemoActivity.class).putExtra(Config.GOODS_PIC_FILE_NAME, tempGoodsDetailInfo), 500);
                break;
            case R.id.ll_img1:
                images1.clear();
                img1.setVisibility(View.GONE);
                img_show1.setVisibility(View.VISIBLE);
                Intent intent1 = new Intent(this, ImageGridActivity.class);
                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES, images1);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent1, 100);
                break;
            case R.id.ll_img2:
                images2.clear();
                img2.setVisibility(View.GONE);
                img_show2.setVisibility(View.VISIBLE);
                Intent intent2 = new Intent(this, ImageGridActivity.class);
                intent2.putExtra(ImageGridActivity.EXTRAS_IMAGES, images2);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent2, 200);
                break;
            case R.id.ll_img3:
                images3.clear();
                img3.setVisibility(View.GONE);
                img_show3.setVisibility(View.VISIBLE);
                Intent intent3 = new Intent(this, ImageGridActivity.class);
                intent3.putExtra(ImageGridActivity.EXTRAS_IMAGES, images3);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent3, 300);
                break;
            case R.id.img_close1:
                images1.clear();
                img1.setVisibility(View.VISIBLE);
                img1.setImageResource(R.drawable.icon_camera);
                img_close1.setVisibility(View.GONE);
                img_show1.setVisibility(View.GONE);
                img_show1.setImageDrawable(null);
                break;
            case R.id.img_close2:
                images2.clear();
                img2.setVisibility(View.VISIBLE);
                img2.setImageResource(R.drawable.icon_camera);
                img_close2.setVisibility(View.GONE);
                img_show2.setVisibility(View.GONE);
                img_show2.setImageDrawable(null);
                break;
            case R.id.img_close3:
                images3.clear();
                img3.setVisibility(View.VISIBLE);
                img3.setImageResource(R.drawable.icon_camera);
                img_close3.setVisibility(View.GONE);
                img_show3.setVisibility(View.GONE);
                img_show3.setImageDrawable(null);
                break;
            case R.id.tv_attribute_add:
                if (goodsAttributes_data.size() >= 5) {
                    ToastUtils.showShort(mContext, "商品属性最多添加5个");
                    return;
                }
                getEditText(goodsAttributes_data, listview, -1);
                goodsAttributes_data.add(new GoodsAttribute());
                adapterGoodsAttributeList.notifyDataSetChanged();
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_save:
                doCommit();
                break;
            case R.id.tv_date_choose:
                DatePickDialog dialog = new DatePickDialog(this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        tvDateChoose.setText(TimeUtil.getStringFromDate(date, "yyyy-MM-dd"));
                    }
                });
                dialog.show();
                break;
            default:
                break;
        }
    }

    private void doCommit() {

        if (StringUtils.isBlank(etGoodsName.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入商品名称");
            return;
        }

        if (StringUtils.isBlank(etKc.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入库存数量");
            return;
        }

        if (StringUtils.isBlank(tvDateChoose.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请选择日期");
            return;
        }


        if (StringUtils.isBlank(etPrice.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入原价");
            return;
        }

        if (StringUtils.isBlank(etLimitNum.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入限购数量");
            return;
        }

        if (StringUtils.isBlank(etGhPrice.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入供货价");
            return;
        }
        if (StringUtils.isBlank(etMemberPrice.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入会员价");
            return;
        }
        if (StringUtils.isBlank(etUnmemberPrice.getText().toString().trim())) {
            ToastUtils.showShort(mContext, "请输入现价");
            return;
        }

//        if (emsMethodId == 0) {
//            ToastUtils.showShort(mContext, "请选择邮寄方式");
//            return;
//        }

        if (idMd.isChecked()){
            if (StringUtils.isBlank(etSettleBl.getText().toString().trim())){
                ToastUtils.showShort(mContext, "请输入结算比例");
                return;
            }
        }

        if (StringUtils.isBlank(firstCategoryId)) {
            ToastUtils.showShort(mContext, "请选择一级类目");
            return;
        }


        if (StringUtils.isBlank(images[0])) {
            ToastUtils.showShort(mContext, "请选择一张主图");
            return;
        }


        String url = Config.SBS_ADD_SERVICE_GOODS;

        RequestParams params = new RequestParams();
        params.put("mshopId", MyApplication.getmInstance().getLoginMEntity().getShopid());
        params.put("groupId", MyApplication.getmInstance().getLoginMEntity().getGroupId());
//        params.put("checkStatus", "2");
        params.put("categoryFirstId", firstCategoryId);
        params.put("categorySecondId", twoCategoryId);
        params.put("name", etGoodsName.getText().toString().trim());
        params.put("validDateString", tvDateChoose.getText().toString().trim());
        params.put("stockNumber", etKc.getText().toString().trim());
        params.put("salePrice", StringUtils.changeY2F(etPrice.getText().toString().trim()));
        params.put("supplyPrice", StringUtils.changeY2F(etGhPrice.getText().toString().trim()));
        params.put("promotionPrice", StringUtils.changeY2F(etMemberPrice.getText().toString().trim()));
        params.put("noVipPrice", StringUtils.changeY2F(etUnmemberPrice.getText().toString().trim()));
//        params.put("postMethod", emsMethodId);
        params.put("limitNumber", etLimitNum.getText().toString().trim());
        params.put("isAnytimeRefund", idCheckOne.isChecked() ? "1" : "0");
        params.put("isAutoRefund", idCheckTwo.isChecked() ? "1" : "0");
        if (idMd.isChecked()) {
            params.put("walletSettleMode", 0);
            params.put("settlementRate", Integer.parseInt(etSettleBl.getText().toString().trim())*100);
        }
        if (idZb.isChecked()) {
            params.put("walletSettleMode", 1);
        }


        String iconPicture = StringUtils.join(images, ",");
        params.put("picture", iconPicture);


        getEditText(goodsAttributes_data, listview, -1);
        if (goodsAttributes_data.size() > 0) {
//            HashMap<String, String> map = (HashMap<String, String>) MapUtil.objectToMap(goodsAttributes_data, null);//new Gson().toJson(goodsAttributes_data);
            HashMap<String, String> map = new HashMap<>();
            for (int i = 0; i < goodsAttributes_data.size(); i++) {
                map.put(goodsAttributes_data.get(i).getGoodsName(), goodsAttributes_data.get(i).getGoodsValue());
            }
            String attribute = new Gson().toJson(map);
            params.put("attribute", attribute);
        } else {
            params.put("attribute", "");
        }


        showProgress("提交中...");

        HttpUtil.post(url, params, new HttpUtil.RequestListener() {

            @Override
            public void success(String response) {
                disShowProgress();
                try {
                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }
                    showErrorMsg(result.getString("message"));
                    finish();


                } catch (Exception e) {
                    e.getMessage();

                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }


    interface ListenerPicCall {
        void getFilePath(String path);
    }

    /**
     * @Description (TODO作用)
     */
    private void uploadPic(final String pic, final ListenerPicCall listener) {


        String url = Config.SBS_UPLOAD_PIC;//ZF_SHOP_SALE_ORDER_EVALUATE;

        RequestParams params = new RequestParams();
        String paths = BMapUtil.fileTofile2(mContext, pic);
        try {
            params.put("file", new File(paths), "image/jpg");
            params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");
            params.put("path", "mallGoods");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        showProgress("提交中...");

        HttpUtil.post(url, params, new HttpUtil.RequestListener() {

            @Override
            public void success(String response) {
                disShowProgress();


                String str = new Gson().fromJson(response, new TypeToken<String>() {
                }.getType());
                listener.getFilePath(str);
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0 && data == null){
            if (requestCode == 100){
                img1.setVisibility(View.VISIBLE);
                img1.setImageResource(R.drawable.icon_camera);
                img_show1.setVisibility(View.GONE);
                img_show1.setImageDrawable(null);
            }else if (requestCode == 200){
                img2.setVisibility(View.VISIBLE);
                img2.setImageResource(R.drawable.icon_camera);
                img_show2.setVisibility(View.GONE);
                img_show2.setImageDrawable(null);
            }else if (requestCode == 300){
                img3.setVisibility(View.VISIBLE);
                img3.setImageResource(R.drawable.icon_camera);
                img_show3.setVisibility(View.GONE);
                img_show3.setImageDrawable(null);
            }
        }
        else if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
                images1 = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);

                uploadPic(images1.get(0).path, new ListenerPicCall() {

                    @Override
                    public void getFilePath(String path) {
                        images[0] = path;
                        img_close1.setVisibility(View.VISIBLE);
                        ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, images1.get(0).path, img_show1, img_show1.getWidth(), img_show1.getHeight());
                    }
                });

            } else if (data != null && requestCode == 200) {
                images2 = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                uploadPic(images2.get(0).path, new ListenerPicCall() {
                    @Override
                    public void getFilePath(String path) {
                        images[1] = path;
                        img_close2.setVisibility(View.VISIBLE);
                        ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, images2.get(0).path, img_show2, img_show2.getWidth(), img_show2.getHeight());
                    }
                });
            } else if (data != null && requestCode == 300) {
                images3 = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                uploadPic(images3.get(0).path, new ListenerPicCall() {
                    @Override
                    public void getFilePath(String path) {
                        images[2] = path;
                        img_close3.setVisibility(View.VISIBLE);
                        ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, images3.get(0).path, img_show3, img_show3.getWidth(), img_show3.getHeight());
                    }
                });
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == 4) {
            if (data != null && requestCode == 400) {
                tempGgInfo = (TempGgInfo) data.getSerializableExtra(Config.GG_FILE_NAME);
            }
        } else if (resultCode == 5) {
            if (data != null && requestCode == 500) {
                tempGoodsDetailInfo = (TempGoodsDetailInfo) data.getSerializableExtra(Config.GOODS_PIC_FILE_NAME);
            }
        } else if (resultCode == 6) {
            if (data != null && requestCode == 600) {

                Bundle bundle = data.getBundleExtra("category");


                String one = bundle.getString("firstCategory");
                String two = bundle.getString("twoCategory");
                firstCategoryId = bundle.getString("firstCategoryId");
                twoCategoryId = bundle.getString("twoCategoryId");

                tv_one_category.setText(StringUtils.isBlank(one) ? "无" : one);
                tv_two_category.setText((StringUtils.isBlank(two) || StringUtils.isEquals(two, "不选")) ? "无" : two);


            }
        }
//        else if (resultCode == 7) {
//            if (data != null && requestCode == 700) {
//                emsMethodId = data.getLongExtra("emsMethodId", 0);
//                String emsName = data.getStringExtra("emsName");
////                tv_yfmb.setText(StringUtils.isBlank(emsName) ? "" : emsName);
//            }
//        }
    }


    @Override
    public boolean onLongClick(View view) {

        switch (view.getId()) {
            case R.id.img_1:
                if (images1 == null) {
                    return false;
                }
                img_close1.setVisibility(View.VISIBLE);
                break;
            case R.id.img_2:
                if (images2 == null) {
                    return false;
                }
                img_close2.setVisibility(View.VISIBLE);
                break;
            case R.id.img_3:
                if (images3 == null) {
                    return false;
                }

                img_close3.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
        return true;
    }


    public static class AdapterGoodsAttributeList extends BaseAdapter {

        private Context context;
        private List<GoodsAttribute> goodsAttributes = new ArrayList<GoodsAttribute>();
        private ListenerDel listenerDel;

        private interface ListenerDel {
            void del(int position);
        }


        public AdapterGoodsAttributeList(Context context, List<GoodsAttribute> ggLists, ListenerDel listenerDel) {
            this.context = context;
            this.goodsAttributes = ggLists;
            this.listenerDel = listenerDel;
        }

        @Override
        public int getCount() {
            return goodsAttributes == null ? 0 : goodsAttributes.size();
        }

        @Override
        public Object getItem(int position) {
            return goodsAttributes.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.listitem_goods_attribute_list, null);

                holder.et_goods_name = (EditText) convertView.findViewById(R.id.et_goods_name);
                holder.et_goods_value = (EditText) convertView.findViewById(R.id.et_goods_value);
                holder.tv_del = convertView.findViewById(R.id.tv_del);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            GoodsAttribute goodsAttribute = goodsAttributes.get(position);

            holder.et_goods_name.setText(goodsAttribute.getGoodsName());
            holder.et_goods_value.setText(goodsAttribute.getGoodsValue());
            holder.tv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listenerDel.del(position);
                }
            });

            return convertView;
        }

        private class ViewHolder {
            EditText et_goods_name;
            EditText et_goods_value;
            TextView tv_del;
        }
    }
}
